package com.moon.game.command.guest.bag;

import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.manager.ItemMG;
import com.moon.game.resource.model.Item;
import com.moon.game.service.ItemService;
import com.moon.game.service.PlayerService;
import com.moon.game.vos.ErrorMsg;
import com.moon.game.vos.PlayerVo;

/**
 * @description 查看装备类道具
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-5-23 下午1:42:33
 */
public class UseAddItemCmd extends BaseCmd {
	
	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
		case STATU_INIT:
			doInit(req);
			break;
		case STATU_CHECK:
			doCheckSelf(req);
			break;
		case STATU_RETURN:
			doResponse(req);
			break;
		default:
			break;
		}
	}
	
	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null) {
			doResponsePsError(req);
			return;
		}
		if (ps.length == 4) {
			int curPage = Integer.parseInt(ps[2]);
			int curType = Integer.parseInt(ps[3]);
			req.setVO("curr_type", curType);
			req.setVO("curr_page", curPage);
		}
		int itemId = Integer.parseInt(ps[0]);
		int num = 1;
		Integer temp = Integer.parseInt(ps[1]);
		if (temp != null) {
			num = temp.intValue();
			if (num < 1 || num > Constants.ONCE_USE_ITEM_MAX_NUM) {
				doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_BEYOND_MAX_NUM, "请输入1~" + Constants.ONCE_USE_ITEM_MAX_NUM + "之间的数字！"));
				return;
			}
		}else {
			doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_BEYOND_MAX_NUM, "请输入1~" + Constants.ONCE_USE_ITEM_MAX_NUM + "之间的数字！"));
			return;
		}
		Item item = ItemMG.instance.getItem(itemId);
		if (item == null) {
			doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_NO_THE_ITEM, "非法请求,不存在你要使用的道具！"));
			return;
		}
		PlayerVo player = getPlayerVo(req);
		StringBuffer info = new StringBuffer();
		int ret = ItemService.delItem(player, itemId, num);
		if (ret == Constants.DEL_ITEM_SUCC) {
			info.append("使用" + item.getName() + "×"+ num + "成功<br/>");
			if (item.getPosition() <= 0) {
				if (item.getHp() > 0) {
					if (player.getCurHp() >= player.getHp()) {
						info.append("气血已满！！<br/>");
					}else {
						info.append("气血增加：" + item.getHp()*num + "<br/>");
						PlayerService.doAddHp(req.getSid(), req.getPlayerId(), item.getHp());
					}
					info.append("气血：" + player.getCurHp() + "/" + player.getHp());
				}
				if (item.getMp() > 0) {
					if (player.getCurMp() >= player.getMp()) {
						info.append("魔法已满！！<br/>");
					}else {
						info.append("魔法增加：" + item.getMp()*num + "<br/>");
						PlayerService.doAddMp(req.getSid(), req.getPlayerId(), item.getMp());
					}
					info.append("魔法：" + player.getCurMp() + "/" + player.getMp());
				}
			}else {
				info.append("该道具目前无法使用！<br/>");
			}
		}else if (ret == Constants.DEL_ITEM_FAIL) {
			info.append("使用" + item.getName() + "异常！<br/>");
		}else if (ret == Constants.DEL_ITEM_NOT_ENOUGH) {
			info.append("您要使用的道具数量不够！<br/>");
		}else if (ret == Constants.DEL_ITEM_LACK) {
			info.append("您当前没有该道具！<br/>");
		}
		req.setVO(TAG_INFO, info.toString());
		response(req, req.getClientResponse());
	}
}
